#
# SMS LIB MAKEFILE
#
# Created by: Phillip Popp (popp.phillip@gmail.com)
# On: April 18th, 2011
#

# output product info
OUTNAME := libsms
OUTDIR := lib
OUTEXT := .a

# compiler flags ect...
INCDIRS := inc ../../gsl/gsl-1.14/ ../../libsndfile/libsndfile-1.0.24/src
SRCDIRS := src
PPDEFINES :=

OBJDIR := obj
DEBUG_OBJDIR := obj/debug
RELEASE_OBJDIR := obj/release

CFLAGS := $(CFLAGS) $(foreach INCDIRS, $(INCDIRS), -I$(INCDIRS))
CFLAGS := $(CFLAGS) $(foreach PPDEFINES, $(PPDEFINES), -D$(PPDEFINES))
CFLAGS := $(CFLAGS) -fPIC
CFLAGS_DEBUG := $(CFLAGS) -g
CFLAGS_RELEASE := $(CFLAGS) -DNDEBUG -O2 -funroll-loops -fomit-frame-pointer -Wall -W -Wno-unused -Wno-parentheses -Wno-switch -fno-strict-aliasing
CC = g++
AR = ar

# get sources
CPPSRCS := $(foreach SRCDIRS, $(SRCDIRS), $(wildcard $(SRCDIRS)/*.cpp))
CSRCS += $(foreach SRCDIRS, $(SRCDIRS), $(wildcard $(SRCDIRS)/*.c))

# get headers
HEADERS := $(foreach SRCDIRS, $(SRCDIRS), $(wildcard $(SRCDIRS)/*.h))
HEADERS += $(foreach INCDIRS, $(INCDIRS), $(wildcard $(INCDIRS)/*.h))

# set search paths for source files
VPATH += $(foreach SRCDIRS, $(SRCDIRS), $(SRCDIRS):)

# set target object files
RELEASE_OBJS := $(patsubst %.c, %.o, $(patsubst %, $(RELEASE_OBJDIR)/%, $(notdir $(CSRCS))))
RELEASE_OBJS += $(patsubst %.cpp, %.o, $(patsubst %, $(RELEASE_OBJDIR)/%, $(notdir $(CPPSRCS))))
DEBUG_OBJS := $(patsubst %.c, %.o, $(patsubst %, $(DEBUG_OBJDIR)/%, $(notdir $(CSRCS))))
DEBUG_OBJS += $(patsubst %.cpp, %.o, $(patsubst %, $(DEBUG_OBJDIR)/%, $(notdir $(CPPSRCS))))


.PHONY: clean debug release dist_clean $(BUILDDEPS)

all: $(BUILDDEPS) debug release
debug: $(OUTDIR)/$(OUTNAME)_debug$(OUTEXT)
release: $(OUTDIR)/$(OUTNAME)$(OUTEXT)
dist_clean: $(BUILDDEPS) clean

clean: 
	rm -f $(DEBUG_OBJS)
	rm -f $(RELEASE_OBJS)
	rm -f $(OUTDIR)/$(OUTNAME)$(OUTEXT)
	rm -f $(OUTDIR)/$(OUTNAME)_debug$(OUTEXT)

$(BUILDDEPS):
	cd $@ && $(MAKE) $(MAKECMDGOALS)

$(OUTDIR)/$(OUTNAME)$(OUTEXT): $(RELEASE_OBJS)
	@mkdir -p $(OUTDIR)
	rm -f $@
	$(AR) -cq $@ $(RELEASE_OBJS) 

$(OUTDIR)/$(OUTNAME)_debug$(OUTEXT): $(DEBUG_OBJS)
	@mkdir -p $(OUTDIR)
	rm -f $@
	$(AR) -cq $@ $(DEBUG_OBJS)

$(DEBUG_OBJDIR)/%.o: %.cpp $(HEADERS)
	@mkdir -p $(OBJDIR)
	@mkdir -p $(DEBUG_OBJDIR)
	$(CC) -c -o $@ $< $(CFLAGS_DEBUG)
	
$(DEBUG_OBJDIR)/%.o: %.c $(HEADERS)
	@mkdir -p $(OBJDIR)
	@mkdir -p $(DEBUG_OBJDIR)
	$(CC) -c -o $@ $< $(CFLAGS_DEBUG)

$(RELEASE_OBJDIR)/%.o: %.cpp $(HEADERS)
	@mkdir -p $(OBJDIR)
	@mkdir -p $(RELEASE_OBJDIR)
	$(CC) -c -o $@ $< $(CFLAGS_RELEASE)

$(RELEASE_OBJDIR)/%.o: %.c $(HEADERS)
	@mkdir -p $(OBJDIR)
	@mkdir -p $(RELEASE_OBJDIR)
	$(CC) -c -o $@ $< $(CFLAGS_RELEASE)
